/**
 * 麦克风权限管理工具类
 */
class MediaPermissions {
    constructor() {
        this.stream = null
        this.permissionStatus = null
    }

    /**
     * 请求麦克风权限
     * @returns {Promise<MediaStream>}
     */
    async requestMicrophonePermission() {
        try {
            this.stream = await navigator.mediaDevices.getUserMedia({
                audio: {
                    channelCount: 1,
                    sampleRate: 16000,
                    echoCancellation: true,
                    noiseSuppression: true
                }
            })
            return this.stream
        } catch (error) {
            console.error('获取麦克风权限失败:', error)
            throw error
        }
    }

    /**
     * 检查麦克风权限状态
     * @returns {Promise<PermissionStatus>}
     */
    async checkPermissionStatus() {
        try {
            this.permissionStatus = await navigator.permissions.query({ name: 'microphone' })
            return this.permissionStatus
        } catch (error) {
            console.error('检查权限状态失败:', error)
            throw error
        }
    }

    /**
     * 停止所有音频轨道
     */
    stopStream() {
        if (this.stream) {
            this.stream.getTracks().forEach(track => track.stop())
            this.stream = null
        }
    }
}

export default new MediaPermissions() 